<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Admin extends Model
{
    //软删除
    use SoftDelete;

    //管理员添加
    public function adminadd($data)
    {
        $validate = new \app\common\validate\Admin();
        if (!$validate->scene('edit')->check($data)){
            return $validate -> getError();
        }
        if (!$data['nickname']){
            $data['nickname'] = nikename();
        }
        $data['password'] = hashs($data['password']);
        $result = $this -> save($data);
        if ($result){
            return 1;
        } else {
            return '添加失败！';
        }
    }

    //管理员状态设置
    public function admin_status($data)
    {
        $admin = $this -> find($data['id']);
        $admin -> status = $data['status']?0:1;
        $result = $admin -> save();
        if ($result) {
            return 1;
        } else {
            return '操作失败！';
        }
    }

    //管理员编辑
    public function admin_edit($data)
    {
        $validate = new \app\common\validate\Admin();
        if (!$validate->scene('adminedit')->check($data)){
            return $validate -> getError();
        }
        $adminInfo = $this -> field('nickname,email') -> find($data['a_id']);
        $adminInfo -> nickname = $data['nickname'];
        $adminInfo -> email = $data['email'];
        $result = $adminInfo -> save();
        if ($result){
            return 1;
        } else {
            return '更新失败！';
        }
    }

    //管理员密码编辑
    public function admin_pass($data)
    {
        if (!$data['oldpassword']){
            return '原密码不能为空！';
        }
        $admin = $this -> find($data['a_id']);
        if (!password_verify($data['oldpassword'],$admin['password'])){
            return '原密码错误！';
        }
        $validate = new \app\common\validate\Admin();
        if(!$validate -> scene('adminpass') -> check($data)){
            return $validate -> getError();
        }
        $admin -> password = hashs($data['qpassword']);
        $result = $admin -> save();
        if ($result){
            return 1;
        } else {
            return '密码修改失败！';
        }
    }

    //后台登录
    public function login($data)
    {
        $validate = new \app\common\validate\Admin();
        if (!$validate->scene('login')->check($data)){
            return $validate -> getError();
        }
        $admin = $this -> where('account',$data['account']) -> find();
        if (!$admin){
            return '该管理员账户不存在！';
        }
        if ($admin['status']==0){
            return '该管理员账户已被禁用！';
        }
        if (!password_verify($data['password'],$admin['password'])){
            return '密码输入错误';
        }
        $system = \model('common/System') -> find();
        $admin = [
            'id' => $admin['id'],
            'account' => $admin['account'],
            'is_super' => $admin['is_super'],
            'nickname' => $admin['nickname'],
            'email' => $admin['email'],
            'webname' => $system['webname'],
            'copyright' => $system['copyright'],
            'icp' => $system['icp'],
            'time' => time() + 1800
        ];
        session('login',$admin);
        return 1;
    }

    //发送验证码
    public function forget($data)
    {
        $validate = new \app\common\validate\Admin();
        if (!$validate->scene('forget')->check($data)){
            return $validate -> getError();
        }
        $result = $this -> where('account',$data['account']) -> find();
        if ($result){
            $num = random(6);
            $code = [
                'num' => $num,
                'account' => $data['account'],
                'settime' => time() + 300
            ];
            session('code',$code);
            mailto($result['email'],'密码重置---随机验证码','您的随机验证码是：'.$num.'(5分钟内有效)');
            return 1;
        } else {
            return '账号不存在！';
        }
    }

    //修改密码
    public function setpass($data)
    {
        if (!$data['code']){
            return '验证码不能为空！';
        }
        if (session('code.settime') < time()){
            return '验证码已过期！';
        }
        if (session('code.num')!=$data['code']){
            return '验证码错误！';
        }
        $validate = new \app\common\validate\Admin();
        if (!$validate->scene('setpass')->check($data)){
            return $validate -> getError();
        }
        $acInfo = $this -> where('account',session('code.account')) -> find();
        $acInfo -> password = hashs($data['qpassword']);
        $result = $acInfo -> save();
        if ($result){
            return 1;
        } else {
            return '修改失败！';
        }
    }
}
